package com.hjm.mall.service.impl;

import com.hjm.mall.api.param.CommentParam;
import com.hjm.mall.api.vo.CommentVO;
import com.hjm.mall.dao.CommentMapper;
import com.hjm.mall.dao.MallUserMapper;
import com.hjm.mall.dao.OrderMapper;
import com.hjm.mall.entity.Comment;
import com.hjm.mall.entity.MallUser;
import com.hjm.mall.entity.Order;
import com.hjm.mall.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    MallUserMapper mallUserMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public List<CommentVO> getGoodsComment(Integer goodsId) {
        List<CommentVO> commentVOList = new ArrayList<>();
        CommentVO commentVO;
        List<Comment> commentList = commentMapper.findGoodsComment(goodsId);
        for (Comment comment:commentList){
            commentVO = new CommentVO();
            BeanUtils.copyProperties(comment,commentVO);
            MallUser mallUser = mallUserMapper.selectByPrimaryKey(comment.getUserId().longValue());
            Order orderById = orderMapper.getOrderById(comment.getOrderId());
            commentVO.setUserName(mallUser.getNickName());      // 设置昵称
            commentVO.setUserPic(mallUser.getProfilePhoto());   // 设置头像
            commentVO.setGoodsType(orderById.getGoodsType());   // 设置评论商品相关订单的款式
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }

    @Override
    public List<CommentVO> getStoreComment(Integer storeId) {
        List<CommentVO> commentVOList = new ArrayList<>();
        CommentVO commentVO;
        List<Comment> commentList = commentMapper.findStoreComment(storeId);
        for (Comment comment:commentList){
            commentVO = new CommentVO();
            BeanUtils.copyProperties(comment,commentVO);
            MallUser mallUser = mallUserMapper.selectByPrimaryKey(comment.getUserId().longValue());
            commentVO.setUserName(mallUser.getNickName());      // 设置昵称
            commentVO.setUserPic(mallUser.getProfilePhoto());   // 设置头像
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }

    @Override
    public Boolean insertComment(Long userId, CommentParam commentParam) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentParam,comment);
        comment.setUserId(userId.intValue());
        return commentMapper.insert(comment) > 0;
    }
}
